package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.goods.entity.goods.GoodsCategoryProperties;
import com.hoshiicloud.goods.entity.goods.GoodsProperties;
import com.hoshiicloud.goods.mapper.goods.GoodsCategoryPropertiesMapper;
import com.hoshiicloud.goods.service.GoodsCategoryPropertiesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linjx on 2019/7/25
 */
@Slf4j
@Service
public class GoodsCategoryPropertiesServiceImpl extends ServiceImpl<GoodsCategoryPropertiesMapper, GoodsCategoryProperties> implements GoodsCategoryPropertiesService {

    /**
     * 根据类目id, 获取该类目下的所有属性id
     */
    @Override
    public List<Long> getProperIdsByCateId(Long id, Long companyId) {

        //  某个类目的 "所有的属性id" GoodsCategoryProperties：类目属性
        QueryWrapper<GoodsCategoryProperties> wrapper = new QueryWrapper<GoodsCategoryProperties>();
        // 设置查询字段
        wrapper.select("properties_id");
        // 添加查询条件
        wrapper.lambda().eq(GoodsCategoryProperties::getCategoryId, id)
                        .eq(GoodsCategoryProperties::getCompanyId, companyId)
                        .eq(GoodsCategoryProperties::getDeleted, 0);
        // 查询
        List<GoodsCategoryProperties> lists = baseMapper.selectList(wrapper);

        if (lists.isEmpty()) {
            return new ArrayList<Long>();
        }
        return lists.stream().map(GoodsCategoryProperties::getPropertiesId).collect(Collectors.toList());
    }

    @Override
    @Cacheable(cacheNames = "GoodsCategoryProperties", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#categoryId")
    @Logable(authName = "商品管理-商品类目规格表管理-获取某商品类目下的属性")
    public List<GoodsProperties> listByCategoryId(Long categoryId) {
        return baseMapper.listGoodsCategoryProByCategoryId(categoryId);
    }

    @Override
    @CacheEvict(cacheNames = "GoodsCategoryProperties", allEntries = true)
    @Logable(authName = "商品管理-商品类目属性表管理-批量新增某商品类目下的属性")
    public Boolean saveBatch1(List<GoodsCategoryProperties> list) {
        return this.saveBatch(list);
    }

    @Override
    @CacheEvict(cacheNames = "GoodsCategoryProperties", allEntries = true)
    @Logable(authName = "商品管理-商品类目属性表管理-批量删除某商品类目下的属性")
    public Integer deleteByCategoryId(Long categoryId) {
        return baseMapper.deleteByCategoryId(categoryId);
    }
}
